package com.lgkj.ui.shop;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.GoodsListdapter;
import com.lgkj.bean.meitao.GoodsListBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.view.MyGridView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/8/20 0020.
 * 内衣鞋类
 */

public class FootActivity extends BaseActivity {
    @BindView(R.id.gv_neiyi)
    MyGridView gvNeiyi;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private GoodsListBean bean;
    int page = 1;
    private GoodsListdapter adapter;
    private int last_page = 2;

    @Override
    public int getLayoutId() {
        return R.layout.activity_foot;
    }

    @Override
    public void initView() {
        setTitle("内衣鞋类");
        adapter = new GoodsListdapter(FootActivity.this);
        gvNeiyi.setAdapter(adapter);
        initDatas();
        gvNeiyi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String goodsId = bean.getData().getData().get(i).getId();
                Intent intent = new Intent(FootActivity.this, ShopDetailsActivivty.class);
                intent.putExtra("goodsId", goodsId);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        srlRefresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                initDatas();

            }
        });
        srlRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                initDatas();
            }
        });
    }

    private void initDatas() {
        if (page > last_page) {
            toastLong("已经是最后一页了", 0);
            finishLoadingData(false);
            return;
        }
        showDiaLog("正在获取数据");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(FootActivity.this));
        map.put("reg_level", "1");
        map.put("type_id", "1");
        map.put("page", page + "");
        HttpUtils.postHttp(map, CommonConstant.goodsList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (page == 1) {
                    adapter.dataClean();
                }
                if (jsonObject.optInt("code") == 0) {
                    finishLoadingData(true);
                    Gson mGson = new Gson();
                    bean = mGson.fromJson(String.valueOf(jsonObject), GoodsListBean.class);
                    if (bean != null && bean.getData() != null && bean.getData().getData() != null) {
                        last_page = bean.getData().getLast_page();
                        adapter.setData(bean.getData().getData());
                    }
                } else {
                    finishLoadingData(false);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
                if (page != 1) {
                    page--;
                    srlRefresh.finishLoadmore(false);
                } else {
                    srlRefresh.finishRefresh(false);
                }
            }
        });
    }

    /**
     * 结束刷新数据
     */
    private void finishLoadingData(boolean succ) {
        if (page == 1) {
            srlRefresh.finishRefresh(succ);
        } else {
            srlRefresh.finishLoadmore(succ);
        }
    }

}
